<template>
  <el-breadcrumb separator="/" id="breadcrumb">
    <el-breadcrumb-item :to="{ path: '/home/log' }">日志管理</el-breadcrumb-item>
    <el-breadcrumb-item>登陆日志</el-breadcrumb-item>
  </el-breadcrumb>
  <tableList
      :dict="dict"
      :tableData="tableData"
      :tableConfig="tableConfig"
      @doit="tableDoit"
  />
  <deleteDialog
      :deleteDialogConfig="deleteDialogConfig"
  />
</template>

<script setup lang="ts">
import tableList from '@/components/TableList.vue'
// import drawer from '@/components/Drawer.vue'
import deleteDialog from '@/components/DeleteDialog.vue'
import {ref, reactive, onMounted} from 'vue'
import {getlogininforList, delLog, exportLog, cleanLog} from "@/api/system/log-login.ts";
import {createDict} from "@/common/dict.ts";
import {ElMessage} from 'element-plus'

//table
import {table_config} from './tableConfig.js'

let tableConfig = reactive(table_config()) //表格配置
let tableData = ref([]) //表格数据
let search = reactive({})



// let drawerData:any = ref({})
let deleteDialogConfig:any = ref({
  visible: false,
  message: "是否删除",
  alertTitle: "警告",
  confirmText: "确认",
  cancelText: "取消",
  confirmFunction: () => {
  },
  cancelFunction: () => {
  }
}) //删除对话框配置
const dictKey = ['sys_request_type'] //本页面所需要的字典值
let dict = reactive({})

onMounted(async () => {
  dict = await createDict(dictKey) //模块字典值初始化
  await dataInit() //数据初始化
})

//数据初始化
async function dataInit() {
  //分页数据加载
  let page = {
    pageNum: tableConfig.pageNum,
    pageSize: tableConfig.pageSize
  }
  let data = Object.assign(page, search)
  //数据初始化
  const r = await getlogininforList(data)
  tableData.value = r.data
  tableConfig.total = r.total
}

let selection:any = [] //多选的数据
//table事件调度中心
async function tableDoit(e) {
  const eventName = e.eventName, val = e.value
  // @ts-ignore
  tell.log('表格事件分析', eventName, val)
  switch (eventName) {
    case 'refresh': //数据重新加载
      await dataInit()
      break;
    case 'expand': //展开或回收列表
      tableConfig.defaultExpandAll = !tableConfig.defaultExpandAll
      break;
    case 'selectionChange': //批量选择
      selection = val
      break;
    case 'del':
      if (selection.length > 0) {
        deleteDialogConfig.value = {
          visible: true,
          message: "是否批量删除选中的日志?",
          alertTitle: "警告",
          confirmText: "确认",
          cancelText: "取消",
          confirmFunction: async () => {
            let query = []
            for (let i in selection) {
              // @ts-ignore
              query.push(selection[i].infoId)
            }
            await delLog(query.toString())
            deleteDialogConfig.value.visible = false
            dataInit()
            ElMessage({
              message: '删除成功',
              type: 'success',
            })
          },
        }
      }
      break;
    case 'clean':
        deleteDialogConfig.value = {
          visible: true,
          message: "是否清空所有登录日志?",
          alertTitle: "警告",
          confirmText: "确认",
          cancelText: "取消",
          confirmFunction: async () => {
            await cleanLog()
            deleteDialogConfig.value.visible = false
            dataInit()
            ElMessage({
              message: '清除成功',
              type: 'success',
            })
          },
        }
      break;
    case 'search': //搜索
      search = val
      dataInit()
      break;
    case 'export': //导出
      exportLog()
      break;
    case 'handleSizeChange':
      tableConfig.pageSize = val
      dataInit()
      break
    case 'handleCurrentChange': //点击分页
      tableConfig.pageNum = val
      dataInit()
      break
    default:
      // @ts-ignore
      tell.log('未知组件事件', eventName, val)
  }
}
</script>
<style scoped lang="scss">
.content {
  padding-top: 20px;
}
</style>